package customerEnter.Frame.JDBCDao.daoFrame;

import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.PagingParametersResult;
import customerEnter.Frame.JDBCDao.EasyBuyDB;
import customerEnter.Frame.Utils.DateUtils;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.SystemJSON;
import customerEnter.Frame.Utils.SystemMD5;
import customerEnter.Frame.ehcache.AvailableEhcache.EcacheJudge;
import customerEnter.Frame.ehcache.AvailableEhcache.EcacheParameterSet;
import customerEnter.Frame.redis.Major.RedisClientOpsForValue;

/**
 * 框架的三層助手類
 * 
 * 类名称：DaoFramMiddlAssistant 类描述： 创建人：dy 创建时间：2016-9-7 下午3:21:26
 * 
 */
public class DaoFramMiddlAssistant<T, K> {
	/**
	 * 把參數添加到each
	 */
	public void SetEachAddParameter(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		RedisJudge(daoFramePameTransmission);
	}

	public void WhileLinkedList(LinkedList<?> values, StringBuffer cen) {
		String str = "";
		for (Object object : values) {
			if ("java.lang.String".equals(object.getClass().getName())) {
				str += "'" + object + "'" + ",";
			} else {
				str += object + ",";
			}
		}
		cen.append(str.substring(0, str.length() - 1));
	}

	/**
	 * 執行語句
	 */
	public Integer setInsertExec(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		System.out.println(daoFramePameTransmission.SQL);
		return EasyBuyDB.getJdbcTemplate().update(daoFramePameTransmission.SQL);
	}

	/**
	 * ehcache 緩存源 放入緩存
	 */
	public void EcacheJudge(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		// 參數添加到緩存
		EcacheParameterSet.getEcacheParameterSet().setEachParameter(
				SystemMD5.EnteMD5(daoFramePameTransmission.SQL),
				SystemJSON.ListToJsonObject(daoFramePameTransmission.DaoFramelistOther),
				EcacheJudge.getEcacheJudge().JsonArrayNullOrIs(
						SystemJSON.ListToJsonObject(daoFramePameTransmission.DaoFramelistOther)),
				daoFramePameTransmission.SQL, DateUtils.getNowTime());
	}

	/**
	 * redis 緩存源 放入緩存 默認緩存存在1分鐘
	 */
	public void RedisJudge(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		try {
			RedisClientOpsForValue.setForValue(SystemMD5.EnteMD5(daoFramePameTransmission.SQL),
					JSON.toJSONString(daoFramePameTransmission.getPagingParametersResult()));
			RedisClientOpsForValue.setOverdueTime(SystemMD5.EnteMD5(daoFramePameTransmission.SQL),
					daoFramePameTransmission.CacheTime, daoFramePameTransmission.timeUnit);
		} catch (Exception e) {
			Log4j.Log4jOutInfo(e.getMessage() + ":緩存沒開啟 /緩存失效/緩存鏈接不對 ");
		}
	}

	@SuppressWarnings("rawtypes")
	public PagingParametersResult getPagingParametersResultCache(String key) {
		return RedisClientOpsForValue.getForValueObject(key);
	}

	/**
	 * 是否有緩存 redis
	 * 
	 * @throws Exception
	 */
	public boolean ifCache(DaoFramePameTransmission<T, K> daoFramePameTransmission) throws Exception {
		boolean bool = false;
		try {
			bool = RedisClientOpsForValue.IfKeyExist(SystemMD5.EnteMD5(daoFramePameTransmission.SQL));
		} catch (Exception e) {
			System.out.println("redis 沒有啟動或者 服務失效");
		}
		return bool;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	void queryCache(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		Log4j.Log4jOutInfo("--------------------查詢緩存-------------------");
		PagingParametersResult pagingParametersResult = getPagingParametersResultCache(SystemMD5
				.EnteMD5(daoFramePameTransmission.SQL));

		Object objectResult = pagingParametersResult.getpObject();

		String value = String.valueOf(objectResult);

		if (JSONArray.class == objectResult.getClass()) {
			List<?> list = JSONArray.parseArray(value, daoFrameDMLBaseTransmission.getClassName());
			pagingParametersResult.setpObject(list);
		} else {
			Object object = JSONObject.parseObject(value, daoFrameDMLBaseTransmission.getClassName());
			pagingParametersResult.setpObject(object);
		}
		daoFramePameTransmission.setPagingParametersResult(pagingParametersResult);
	}

}
